/*
 * Copyright 2014 Alex Curran
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.amlcurran.showcaseview;

import ohos.agp.render.BlendMode;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.render.Texture;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.media.image.PixelMap;

/**
 * 基础样式引导层，镂空圆形
 */
public class MaterialShowcaseDrawer implements ShowcaseDrawer {
    private float radius;
    private final Paint basicPaint;
    private final Paint eraserPaint;
    private int backgroundColor;

    public MaterialShowcaseDrawer(Context context) {
        try {
            this.radius = context.getResourceManager()
                    .getElement(ResourceTable.Float_showcase_radius_material).getFloat();
        } catch (Exception e) {
            LocalHiLog.warn("ShowcaseView", "showcase_radius not found");
        }
        this.eraserPaint = new Paint();
        this.eraserPaint.setColor(new Color(0xFFFFFF));
        this.eraserPaint.setAlpha(0);
        this.eraserPaint.setBlendMode(BlendMode.CLEAR);
        this.eraserPaint.setAntiAlias(true);
        this.eraserPaint.setStyle(Paint.Style.FILLANDSTROKE_STYLE);
        this.basicPaint = new Paint();
    }

    @Override
    public void setShowcaseColour(int color) {
    }

    @Override
    public void drawShowcase(PixelMap buffer, float startX, float startY, float scaleMultiplier) {
        Texture texture = new Texture(buffer);
        Canvas bufferCanvas = new Canvas(texture);
        bufferCanvas.drawCircle(startX, startY, radius, eraserPaint);
    }

    @Override
    public void drawShowcaseBorder(Canvas canvas, float x, float y) {
    }

    @Override
    public int getShowcaseWidth() {
        return (int) (radius * 2);
    }

    @Override
    public int getShowcaseHeight() {
        return (int) (radius * 2);
    }

    @Override
    public float getBlockedRadius() {
        return radius;
    }

    @Override
    public void setBackgroundColour(int backgroundColor) {
        this.backgroundColor = backgroundColor;
    }

    @Override
    public void erase(PixelMap bitmapBuffer) {
        bitmapBuffer.writePixels(backgroundColor);
    }

    @Override
    public void drawToCanvas(Canvas canvas, PixelMap bitmapBuffer) {
        PixelMapHolder pixelMapHolder = new PixelMapHolder(bitmapBuffer);
        canvas.drawPixelMapHolder(pixelMapHolder, 0, 0, basicPaint);
    }
}
